package com.attendance.manage.service.impl;

import com.attendance.common.core.domain.AjaxResult;
import com.attendance.manage.domain.Attendance;
import com.attendance.manage.domain.AttendanceStatDTO;
import com.attendance.manage.service.IAttendanceService;
import com.attendance.manage.service.ICommonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import static com.attendance.common.core.domain.AjaxResult.success;
import static com.attendance.common.utils.SecurityUtils.getLoginUser;
@Service
public class ICommonsServiceImpl implements ICommonsService {
    @Autowired
    private IAttendanceService attendanceService;

    @Override
    public AjaxResult selectmonth() {
        String username = getLoginUser().getUser().getNickName();
        Attendance attendance = new Attendance();
        attendance.setUsername(username);
        //将ClockInTime设置为当前月份第一天
        Date startDate = Date.from(LocalDate.now().withDayOfMonth(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        attendance.setClockInTime(startDate);
        System.out.println("startDate:" + startDate);
        //将ClockInTime设置为今天23 点
        Date endDate = Date.from(LocalDate.now().atTime(23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
        attendance.setClockOutTime(endDate);
        System.out.println("endDate:" + endDate);
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        AttendanceStatDTO attendanceStatDTO = new AttendanceStatDTO();
        attendanceStatDTO.setLateCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("迟到")).count());
        attendanceStatDTO.setEarlyLeaveCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("早退")).count());
        attendanceStatDTO.setAbsentCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("旷工")).count());
        System.out.println(attendanceStatDTO);
        return success(attendanceStatDTO);
    }
}
